<template>
    <el-card shadow="always">
        {{specSearch}}
        <!--1. 条件查询-->
        <el-form :inline="true" :model="specSearch" class="demo-form-inline">
            <el-form-item label="">
                <el-input v-model="specSearch.specName" placeholder="规格名字" size="small"></el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="search" icon="el-icon-search" size="small">查询</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="success" @click="addUI" icon="el-icon-plus" size="small" plain>添加</el-button>
            </el-form-item>
            <el-form-item>
                <el-button type="warning" @click="handleDelete" icon="el-icon-delete" size="small" plain>批量删除</el-button>
            </el-form-item>
        </el-form>
        <!--2. 数据展示-->
        <el-table
                :data="specifications"
                @selection-change="handleSelectionChange">
                style="width: 100%">
            <el-table-column
                    type="selection"
                    width="55">
            </el-table-column>
            <el-table-column
                    align="center"
                    label="规格Id"
                    prop="id"/>

            <el-table-column
                    align="center"
                    label="规格名称"
                    prop="specName"/>

            <el-table-column label="操作" align="center">
                <template slot-scope="scope">
                    <!--<el-button-->
                    <!--        size="mini"-->
                    <!--        @click="handleEdit(scope.$index, scope.row)">编辑</el-button>-->
                    <!--<el-button-->
                    <!--        size="mini"-->
                    <!--        type="danger"-->
                    <!--        @click="handleDelete(scope.$index, scope.row)">删除</el-button>-->
                    <!--使用按钮组代替上面的单个按钮-->
                    <el-button-group>
                        <el-button type="success" size="small" icon="el-icon-edit"
                                   @click="handleEdit(scope.$index, scope.row)"/>
                        <el-button type="danger" size="small" icon="el-icon-delete"
                                   @click="handleDelete(scope.$index, scope.row)"/>
                    </el-button-group>
                </template>

            </el-table-column>
        </el-table>
        <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="page"
                :page-sizes="[5, 10, 15, 20]"
                :page-size="pageSize"
                layout="total, sizes, prev, pager, next, jumper"
                :total="total">
        </el-pagination>

        <el-dialog :title="specGroup.id ? '修改规格' : '添加规格'" :visible.sync="dialogFormVisible">
            {{specGroup}}
            <el-form :model="specGroup">
                <el-form-item label="规格名称" :label-width="formLabelWidth">
                    <el-input v-model="specGroup.specName" autocomplete="off"></el-input>
                </el-form-item>
            </el-form>
            <el-button type="success" plain size="small" @click="specGroup.options.push({})">新增规格选项</el-button>

            <el-table
                    :data="specGroup.options"
                    style="width: 100%">
                <el-table-column
                        label="规格选项"
                        align="center">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.optionName"/>
                    </template>
                </el-table-column>
                <el-table-column
                        align="center"
                        label="排序">
                    <template slot-scope="scope">
                        <el-input v-model="scope.row.orders"/>
                    </template>
                </el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button
                                size="mini"
                                type="danger"
                                @click="specGroup.options.splice(scope.$index,1)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="save">确 定</el-button>
            </div>
        </el-dialog>
    </el-card>
</template>

<style>
    .el-card {

        width: 80% !important;
        margin: 50px auto !important;

    }

    .el-pagination {
        text-align: right;
        margin-top: 30px;
    }
</style>
<script>
    import {findAll, findByPage, search, findById, save,delSpec} from "@/api/specification"

    export default {
        data() {
            return {
                specifications: [],             // 代表所有规格列表
                page: 1,                         // 代表当前页
                pageSize: 5,                     // 代表每页大小
                total: 100,                      // 代表总记录数
                specSearch: {},                  // 规格查询
                formLabelWidth: '100px',         // 设置文本的宽度
                specGroup: {specName: '', options: []},          // 规格及规格选项的组合对象
                dialogFormVisible: false,        // 对话框显示与隐藏
                multipleSelection: [],           // 复选框数组
                ids:[],                          // 代表要删除的规格id列表

            }
        },
        created() {
            //1. 查询所有的规格
            // this.findAll()
            //2. 分页查询规格
            // this.findByPage()
            //3. 分页带条件查询
            this.search()
        },
        methods: {
            //1. 保存规格及规格选项
            save() {
                //1.1 根据specGroup.id是否有值确定是添加还是修改
                let url = "/specification/add.do"
                if (this.specGroup.id) {
                    url = "/specification/update.do"
                }
                let _this = this
                //1.2 开始提交
                save(url, this.specGroup).then(({data}) => {
                    if(data.code === 0){
                        this.$message({
                            message: data.msg,
                            type: 'success',
                            onClose(){
                                //1. 关闭对话框
                                _this.dialogFormVisible = false
                                //2. 刷新页面
                                _this.search()
                            }
                        });
                    }else{
                        this.$message({
                            message: data.errors,
                            type: 'error'
                        });
                    }

                })

            },
            //点击复选框按钮时触发
            handleSelectionChange(val) {
                this.multipleSelection = val;
                console.log("选择项：",this.multipleSelection)
            },
            //1. 分页带条件查询
            search() {
                search(this.page, this.pageSize, this.specSearch).then(({data}) => {
                    //1.1 为总记录数赋值
                    this.total = data.pr.total
                    //1.2 为每页记录集合赋值
                    this.specifications = data.pr.rows
                    console.log("data:", data)
                })
            },
            //2. 添加规格的对话框显示
            addUI() {
                //2.1 弹出对话框
                this.dialogFormVisible = true
                //2.2 清空数据
                this.specGroup = {specName: '', options: []}
            },
            //1. 分页查询规格
            findByPage() {
                findByPage(this.page, this.pageSize).then(({data}) => {
                    //1.1 为总记录数赋值
                    this.total = data.pr.total
                    //1.2 为每页记录集合赋值
                    this.specifications = data.pr.rows
                })
            },
            //1. 查询所有规格
            findAll() {
                findAll().then(({data}) => {
                    console.log("data:", data)
                    this.specifications = data.specifications
                })
            },
            //2. 编辑规格
            handleEdit(index, row) {
                console.log(index, row);
                //2.0 弹出对话框
                this.dialogFormVisible = true
                //2.1 根据规格id查询出规格名字及规格选项列表
                findById(row.id).then(({data}) => {
                    console.log("data:", data)
                    this.specGroup = data.group
                    this.specGroup.id = row.id    // 为后面执行的是添加还是修改操作准备！
                })
            },
            //删除单个或批量删除
            //问题？如何区分是单个删除还是批量删除？根据传参row的值是否有值决定，如果有值，说明点击了删除按钮，此时删除的是单个值，否则，删除的是多个？
            handleDelete(index, row) {
                console.log(index, row);
                //第一步：确定ids数组的值
                if(row){        // 此时是单击了单个删除按钮
                    this.ids = [row.id]
                }else{          // 此时代表多选
                    this.ids = this.multipleSelection.map(m=>m.id)
                }
                //第二步：进行批量或单个删除
                let _this = this
                this.$confirm(`是否要删除编号为：${this.ids}的规格？`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {         // 点击了确定按钮时，调用后台的删除方法
                    delSpec(this.ids).then(({data}) => {
                        if(data.code === 0){
                            this.$message({
                                type: 'success',
                                message: data.msg,
                                onClose(){
                                    _this.search()
                                }
                            });
                        }
                    })

                }).catch(() => {        // 点击了取消按钮
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            },
            // 页大小pageSize改变时触发
            handleSizeChange(val) {
                console.log(`每页 ${val} 条`);
                this.pageSize = val
                this.findByPage()
            },
            // 当前页page改变时触发
            handleCurrentChange(val) {
                console.log(`当前页: ${val}`);
                this.page = val
                this.findByPage()
            }
        }
    }
</script>